package com.heima.behavior.service.impl;


import com.heima.api.user.IUserClient;
import com.heima.behavior.mapper.ApBehaviorMapper;
import com.heima.behavior.service.IApBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.behavior.dtos.ArticleInfoDto;
import com.heima.model.behavior.dtos.BehaviorDto;
import com.heima.model.behavior.dtos.CollectionBehaviorDto;
import com.heima.model.behavior.pojos.ApBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.utils.common.WmThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chen
 * @since 2024-11-26
 */
@Service
@Slf4j
public class ApBehaviorServiceImpl extends ServiceImpl<ApBehaviorMapper, ApBehavior> implements IApBehaviorService {
    @Autowired
    private  IUserClient userClient;
    @Override
    public void saveLikesBehavior(BehaviorDto dto) {
        // 判断是否为评论点赞
        if(dto.type == 2){
            return;
        }
        //判断是否存在数据
        ApBehavior apBehavior = checkExists(dto.getArticleId(),WmThreadLocalUtil.getUser().getId());
        if(apBehavior!= null){
            // 存在,更新数据库点赞状态
            apBehavior.setIsLikes(dto.getOperation());
            apBehavior.setUpdateTime(LocalDateTime.now());
            this.updateById(apBehavior);
            return;
        }
        // 不存在,保存点赞行为
        ApBehavior behavior = new ApBehavior();
        behavior.setArticleId(dto.getArticleId());
        behavior.setUserId(WmThreadLocalUtil.getUser().getId());
        behavior.setType(dto.type);
        behavior.setIsLikes(dto.getOperation());
        behavior.setCreateTime(LocalDateTime.now());
        this.save(behavior);
    }

    @Override
    public void saveReadBehavior(BehaviorDto dto) {
        // 判断是否存在数据
        ApBehavior apBehavior = checkExists(dto.getArticleId(),WmThreadLocalUtil.getUser().getId());
        if(apBehavior!= null){
            // 存在,更新数据库阅读状态
            apBehavior.setCount(apBehavior.getCount() + 1);
            apBehavior.setUpdateTime(LocalDateTime.now());
            this.updateById(apBehavior);
            return;
        }
        // 不存在,保存阅读行为
        ApBehavior behavior = new ApBehavior();
        behavior.setArticleId(dto.getArticleId());
        behavior.setUserId(WmThreadLocalUtil.getUser().getId());
        behavior.setType(dto.type);
        behavior.setCount(1);
        behavior.setCreateTime(LocalDateTime.now());
    }

    @Override
    public void saveUnLikesBehavior(BehaviorDto dto) {
        // 判断是否存在数据
        ApBehavior apBehavior = checkExists(dto.getArticleId(),WmThreadLocalUtil.getUser().getId());
        if(apBehavior!= null){
            // 存在,更新数据库不喜欢状态
            apBehavior.setIsUnlikes(dto.getType());
            apBehavior.setUpdateTime(LocalDateTime.now());
            this.updateById(apBehavior);
            return;
        }
        // 不存在,保存不喜欢行为
        ApBehavior behavior = new ApBehavior();
        behavior.setArticleId(dto.getArticleId());
        behavior.setUserId(WmThreadLocalUtil.getUser().getId());
        behavior.setIsUnlikes(dto.getType());
        behavior.setCreateTime(LocalDateTime.now());
        this.save(behavior);
    }

    @Override
    public void saveCollectionBehavior(CollectionBehaviorDto dto) {
        // 判断是否存在数据
        ApBehavior apBehavior = checkExists(dto.getEntryId(),dto.getUserId());
        if(apBehavior!= null) {
            // 存在,更新数据库收藏状态
            apBehavior.setIsCollection(dto.getOperation());
            apBehavior.setUpdateTime(LocalDateTime.now());
            this.updateById(apBehavior);
            return;
        }
        // 不存在,保存收藏行为
        ApBehavior behavior = new ApBehavior();
        behavior.setArticleId(dto.getEntryId());
        behavior.setUserId(WmThreadLocalUtil.getUser().getId());
        behavior.setCreateTime(LocalDateTime.now());
        this.save(behavior);
    }

    @Override
    public Object loadArticleBehavior(ArticleInfoDto dto) {
        // 判断是否存在数据
        ApBehavior apBehavior = checkExists(dto.getArticleId(),dto.getUserId());
        Map<String,Boolean> map =new HashMap<>();
        map.put("islike",false);
        map.put("isunlike",false);
        map.put("iscollection",false);
        map.put("isfollow",false);
        if(apBehavior!= null){
            // 存在,返回数据
            map.put("islike",apBehavior.getIsLikes()==0);
            map.put("isunlike",apBehavior.getIsUnlikes()==0);
            map.put("iscollection",apBehavior.getIsCollection()==0);
        }
        // 调用用户服务判断是否关注
        ResponseResult responseResult = userClient.userFollowInfo(dto);
        map.put("isfollow",responseResult.getData()!=null);
        log.info("用户行为微服务loadArticleBehavior:{}",map);
        return map;
    }

    public ApBehavior checkExists(Long articleId, Integer userId) {
        return this.lambdaQuery().eq(ApBehavior::getArticleId,articleId).eq(ApBehavior::getUserId, userId).one();
    }
}
